/*
 * FreeSpaceFile.h
 *
 */

#ifndef FREESPACEFILE_H_
#define FREESPACEFILE_H_

#include <fstream>
#include <exception>
#include <vector>
#include "../BinaryFile.h"

class FreeSpaceFileException : public std::exception {
	private:
		std::string msg;
	public:
		FreeSpaceFileException(const std::string& msg) throw();
		virtual ~FreeSpaceFileException() throw();
		const char* what() const throw();
};

class FreeSpaceFile : public BinaryFile {
	private:
		typedef std::vector<BigInt> FreeBlockVector;
		FreeBlockVector blocks;

		const BigInt blockSize;
		const BigInt blocksPerRead;
		BigInt indexBeg;
		BigInt blocksRead;

	public:
		FreeSpaceFile(BigInt blocksPerRead, BigInt blockSize);
		virtual ~FreeSpaceFile();

		void push(BigInt blockFreeSpace);
		BigInt findSpace(BigInt amount);
		BigInt getFreeSpace(BigInt block);
		void setFreeSpace(BigInt block, BigInt amount);

	protected:
		void rewind();
		bool readFromFile();
		void writeToFile();
		bool findBufferContainingBlock(BigInt block);
		void logBuffer();
};

#endif /* FREESPACEFILE_H_ */
